package com.polaris.live.adapter.music

import android.view.LayoutInflater
import android.view.ViewGroup
import com.blankj.utilcode.util.StringUtils
import com.bumptech.glide.Glide
import com.bumptech.glide.load.resource.bitmap.CenterCrop
import com.bumptech.glide.load.resource.bitmap.RoundedCorners
import com.chad.library.adapter.base.viewholder.BaseViewHolder
import com.polaris.live.R
import com.polaris.live.common.adapter.BaseAdapter
import com.polaris.live.common.ext.autoDp
import com.polaris.live.databinding.AdapterMusicMainItemBinding
import com.polaris.live.translation.R.string
import com.polaris.live.utils.zego.media.MediaProxy
import com.polaris.live.utils.zego.media.MusicBean
import com.polaris.live.utils.zego.media.MusicState

/**
 *
 * @author Created by 天晴 on 2024/9/18 11:15
 * @since 1.0.0
 **/
class MusicMainItemAdapter : BaseAdapter<MusicBean, MusicMainItemHolder>(0) {

    private var mCurrentIndex = -1 //当前播放的条目位置
    private var mCurrentState = MusicState.UNREADY //当前播放状态

    init {
        addChildClickViewIds(R.id.music_rm, R.id.music_play)
    }

    override fun onCreateDefViewHolder(
        parent: ViewGroup,
        viewType: Int,
    ): MusicMainItemHolder {
        return MusicMainItemHolder(
            AdapterMusicMainItemBinding.inflate(LayoutInflater.from(parent.context), parent, false)
        )
    }

    override fun convert(holder: MusicMainItemHolder, item: MusicBean) {
        holder.binding.apply {
            songTv.text = "${StringUtils.getString(string.song_title)}:"
            singerTv.text = "${StringUtils.getString(string.singer)}:"
            musicName.text = item.name
            musicPlayer.text = item.artist
            musicTime.text = formatTime(item.duration)
            if (holder.absoluteAdapterPosition == mCurrentIndex) {
                when (MediaProxy.getCurrentMusicState()) {
                    MusicState.UNREADY,
                    MusicState.READY,
                    MusicState.STOPING,
                    MusicState.PAUSE,
                    MusicState.PLAY_ENDED,
                    MusicState.FAIL,
                    -> {
                        musicPlay.setImageResource(R.drawable.music_play)
                    }

                    MusicState.PLAYING -> {
                        musicPlay.setImageResource(R.drawable.music_playing)
                    }
                }
            } else {
                musicPlay.setImageResource(R.drawable.music_play)
            }
            Glide.with(context)
                .load(item.getAlbumCoverFromMusicFile())
                .placeholder(R.drawable.unknown_cover)
                .error(R.drawable.unknown_cover)
                .transform(CenterCrop(), RoundedCorners(6.autoDp))
                .into(musicPlayerCover)
        }

    }

    private fun formatTime(millis: Long): String {
        val seconds = millis / 1000
        val hours = seconds / 3600
        val minutes = (seconds % 3600) / 60
        val secs = seconds % 60

        return when {
            hours > 0 -> String.format("%02d:%02d:%02d", hours, minutes, secs)
            minutes > 0 -> String.format("%02d:%02d", minutes, secs)
            else -> String.format("00:%02d", secs)
        }
    }

    /**
     * 切换了音乐条目
     */
    fun switchMusic(musicBean: MusicBean) {
        val index = data.indexOfFirst { it.id == musicBean.id }
        val lastIndex = mCurrentIndex
        if (mCurrentIndex != index) {
            mCurrentIndex = index
            notifyItemChanged(lastIndex)
            notifyItemChanged(index)
        }
        mCurrentState = MediaProxy.getCurrentMusicState()
    }

    fun musicState(state: Int) {
        if (mCurrentState != state) {
            notifyItemChanged(mCurrentIndex)
        }
    }

    fun getCurrentIndex(): Int {
        return mCurrentIndex
    }
}

class MusicMainItemHolder(
    val binding: AdapterMusicMainItemBinding,
) : BaseViewHolder(binding.root)
